package org.thanos.simulator;

import com.digitalpetri.modbus.client.ModbusTcpClient;
import com.digitalpetri.modbus.exceptions.ModbusExecutionException;
import com.digitalpetri.modbus.exceptions.ModbusResponseException;
import com.digitalpetri.modbus.exceptions.ModbusTimeoutException;
import com.digitalpetri.modbus.pdu.ReadCoilsRequest;
import com.digitalpetri.modbus.pdu.ReadCoilsResponse;
import com.digitalpetri.modbus.tcp.client.NettyTcpClientTransport;

import java.util.Arrays;

public class ModbusApiTest {

    private static ModbusTcpClient client ;

    public static void initClient(String host, int port) throws ModbusExecutionException {
        NettyTcpClientTransport transport = NettyTcpClientTransport.create(cfg -> {
            cfg.setHostname(host);
            cfg.setPort(port);
        });
        ModbusTcpClient client = ModbusTcpClient.create(transport);
        client.connect();
    }

    public static void main(String[] args) {

    }

    public static void readCoil(int unitId,int address,int quantity) throws ModbusExecutionException, ModbusTimeoutException, ModbusResponseException {
        ReadCoilsResponse response = client.readCoils(unitId,new ReadCoilsRequest(address,quantity));
        System.out.println(Arrays.toString(response.coils()));
    }
}
